/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Last Change:
 *    by:   $Author: $
 *    date: $Date:   $
 * Copyright (c): Johannes Kuhaupt
 * Licence: Creative Commons CC BY-NC-SA 3.0
 */
package control;


import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.fife.ui.rsyntaxtextarea.FileLocation;
import view.CustomEditorPane;
import view.MainFrame;

/**
 *
 * @author Johannes Kuhaupt
 */
public class FileManager {
    
    private File activeFile=null;
    private GuiManager gui;
    private final CustomEditorPane textEditor;    

    
    
    /**
     * Constructor FileManger. Needs Guimanager object as parameter
     * @param pGui
     */
    public FileManager(CustomEditorPane pEditor, File pFile){
        this.gui = GuiManager.getInstance();
        this.textEditor=pEditor;
        this.activeFile = pFile;
        
    }
    
    
    
    /**
     * Discover the extension of a file
     * @param f a file object
     * @return ext the extension of the file (no extension -> null)
     */
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');
 
        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
    
    public static boolean hasWebExtension(File pFile){
        if(getExtension(pFile)!=null){
            if(getExtension(pFile).equals("html")||getExtension(pFile).equals("htm")||getExtension(pFile).equals("css")){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }        
        
    }
    
    public static boolean hasHtmlExtension(File pFile){
        if(getExtension(pFile)!=null){
            if(getExtension(pFile).equals("html")||getExtension(pFile).equals("htm")){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }        
        
    }
    
    public static boolean hasCssExtension(File pFile){
        if(getExtension(pFile)!=null){
            if(getExtension(pFile).equals("css")){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }        
        
    }
    
    public void handleSaveFileAction(){
        this.saveFile();
        
    }
    
    public void handleCloseFileAction(){
        if(this.textEditor.isDirty()&& this.textEditor.getDocument().getLength()>0){
            if (this.gui.showYesNoMessage(GuiManager.getLangBundle().getString("MsgInfoSaveFile"), GuiManager.getLangBundle().getString("MsgInfoTitleSave"))) {
                this.saveFile();
            }
        }
        
        this.activeFile = null;
        this.textEditor.setText("");
        //this.gui.setInfoTextStatusBar(true);
    }
    
    
    
    public void handleOpenFileAction(){
        if(this.activeFile!=null){
            if (this.openFileInNewTab()) {
                this.gui.setInfoTextStatusBarExistingFile(activeFile.getAbsolutePath());
                if(FileManager.hasHtmlExtension(activeFile)){
                    this.gui.showHTML();
                }
                
            }
        }
        else{
            if (this.openFileInActiveTab()) {
                this.gui.setInfoTextStatusBarExistingFile(activeFile.getAbsolutePath());
                this.gui.setActiveTabHeader(activeFile.getName());
                if(FileManager.hasHtmlExtension(activeFile)){
                    this.gui.showHTML();
                }
                
            }
            
        }
        
    }
    
    public void handleNewCloseFileAction(){
        this.gui.openNewTabForNewFile();
    }
    
    /**
     *
     * @return
     */
    public boolean openFileInNewTab(){       
        File f = this.gui.showOpenFileDialog();
        if(f!=null){
                this.gui.openNewTabForExistingFile(f);                
                return true;
        }
        
        else{
            return false;
        }       
        
    }
    
    public boolean openFileInActiveTab(){       
        File f = this.gui.showOpenFileDialog();
        if(f!=null){
                this.activeFile=f;
            try {
                this.textEditor.loadAndShowFile(this.activeFile);
            } catch (IOException ex) {
                Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
            }
                return true;
        }
        
        else{
            return false;
        }       
        
    }
    
    

    
   
    

    
    /**
     *
     */
    public void saveFileAs(){
        
        File f = null;
        boolean userAction = false;
        while(!userAction){
           f = this.gui.showSaveFileDialog();
           
           if(f!=null){
               if(f.exists()){
                    if(this.gui.showYesNoMessage(GuiManager.getLangBundle().getString("MsgInfoExistingFile"), GuiManager.getLangBundle().getString("MsgInfoTitle"))){
                        userAction = true;
                    }
                }
               else{
                   userAction = true;
               }
           }
           else{
               userAction = true;
           }
           
           
        }
            if(f!=null){
                
                try {
                    
                    this.textEditor.saveAs(FileLocation.create(f));
                    this.activeFile = f;
                    this.gui.setInfoTextStatusBarExistingFile(this.activeFile.getAbsolutePath());
                    this.gui.setActiveTabHeader(this.activeFile.getName());
                } catch (IOException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                    this.gui.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorFileSaving")+"\n"+ex.toString());
                }
            }
    }
    
    /**
     *
     */
    public void saveFile(){
        if(this.activeFile !=null){
            try {
                this.textEditor.save();
            } catch (IOException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                this.gui.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorFileSaving")+"\n"+ex.toString());
            }
        }
        else{
            //File f = this.gui.showSaveFileDialog();
            //if(f!=null){
             //   try {                    
             //       this.textEditor.saveAs(FileLocation.create(f));
             //       this.activeFile = f;
             //   } catch (IOException ex) {
             //       Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
             //       this.gui.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorFileSaving")+"\n"+ex.toString());
             //   }
            this.saveFileAs();
             //   this.gui.setInfoTextStatusBarExistingFile(this.activeFile.getAbsolutePath());
        //this.gui.setActiveTabHeader(this.activeFile.getName());
                
          //  }
        }
        
    }

    public void handleNewFileAction() {
            this.gui.openNewTabForNewFile();
    }
   
    
    
}


